package edu.cs346.group5.logic;

import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.Topic;

public class Profile {

	public static void SaveProfileUrl(String url, int userId) {
		try {
			Database.execRaw("Update Users set profile_picture = '" + url
					+ "' Where Id = '" + userId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static List<HashMap<String, Object>> GetWishlistByUserId(int userId) {
		List<HashMap<String, Object>> results = null;
		try {
			results = Database
					.select("Select * From Wishlists w INNER JOIN Items i ON w.item_id = i.id INNER JOIN Posts p ON p.item_id = i.id Where user_id = '"
							+ userId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return results;
	}

	public static void DeleteTopicFromWishList(int userId, int itemId) {
		try {
			Database.execRaw("Delete From Wishlists Where user_id = '" + userId
					+ "' AND item_id = '" + itemId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Topic[] GetTopicsCreatedByUser(int userId) {
		List<HashMap<String, Object>> results = null;
		try {
			results = Database
					.select("Select * From Items i INNER JOIN Posts p ON i.id = p.item_id Where p.posted_by = '"
							+ userId + "' AND in_reply_to is null ORDER BY posted_date DESC");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Topic[] topics = new Topic[results.size()];
		for (int i = 0; i < topics.length; i++) {
			Topic temp = new Topic();
			temp.setCategoryId((int) results.get(i).get("category"));
			temp.setDescription(results.get(i).get("description").toString());
			temp.setImageUrl(results.get(i).get("image").toString());
			temp.setReferenceURL(results.get(i).get("reference_url").toString());
			temp.setTopicName(results.get(i).get("content").toString());
			temp.setItemId((int) results.get(i).get("item_id"));
			topics[i] = temp;
		}
		return topics;
	}

	public static Topic[] GetTopicsCommentedByUser(int userId) {
		List<HashMap<String, Object>> results = null;
		try {
			results = Database
					.select("Select * From Posts p Where p.posted_by = '" + userId + "' AND in_reply_to IS NOT null Group By in_reply_to ORDER BY posted_date desc");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Topic[] topics = new Topic[results.size()];
		for (int i = 0; i < topics.length; i++) {
			Topic temp = new Topic();
			temp.setTopicName(results.get(i).get("content").toString());
			temp.setItemId((int)results.get(i).get("in_reply_to"));
			topics[i] = temp;
		}
		return topics;
	}
	public static void BanUser(Date endBanDate, int userId){
		try {
			Database.execRaw("Update Users Set ban_end_date = '" + endBanDate + "' Where id = '" + userId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void RemoveBan(int userId){
		try {
			Database.execRaw("Update Users Set ban_end_date = null Where id = '" + userId + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

}
